// Lang_38 [exception handling].nova // The application class. class ExceptionHandlingApp { // Application class's 'main' function. public static void main( String[] args ) { // Exception handler 'try' block. try { // Output a message. Stream.writeLine( "Inside the 'try' block :)" ); // Throw an exception. throw new ExceptionSubClass( "Testing 123" ); // Output a message. Stream.writeLine( "No exception has been thrown in the try block." ); } // Exception sub class catch block. catch ( ExceptionSubClass e ) { // Output a message. Stream.writeLine( "Caught an exception of type 'ExceptionSubClass'." ); Stream.writeLine( "e.info = " + e.getInfo( ) ); } // Generic classes must come after specific sub classes. catch ( Exception e ) { // Output a message. Stream.writeLine( "Caught an exception of type 'Exception'." ); Stream.writeLine( "e.info = " + e.getInfo( ) ); } // Exception handler 'finally' block. finally { // Output a message. Stream.writeLine( "Inside the 'finally' block :)" ); } // Test an exception raised inside a constructor call. TestConstructorException testConstructorException; try { testConstructorException = new TestConstructorException( ); } catch ( ExceptionSubClass e ) { // Output a message. Stream.writeLine( "Caught an exception of type 'ExceptionSubClass'." ); Stream.writeLine( "e.info = " + e.getInfo( ) ); if ( testConstructorException == null ) { Stream.writeLine( "testConstructorException == null" ); } } // Produce an unhandled exception to demonstrate the stack trace. // NOTE: This will terminate the program! // Call the first stack trace test method. testStackTrace1( ); } private static void testStackTrace1( ) { testStackTrace2( ); } private static void testStackTrace2( ) { testStackTrace3( ); } private static void testStackTrace3( ) { testStackTrace4( ); } private static void testStackTrace4( ) { // Output a message. Stream.writeLine( "Throw a test exception to test the stack trace." ); // Throw a test exception to test the stack trace. throw new ExceptionSubClass( "Exception to test the stack trace." ); } } // 'Exception' sub class test class. class ExceptionSubClass : Exception { // Constructor. public ExceptionSubClass( String info ) : Exception( info ) { // No code required. } } // Class to test an exception in a constructor call. class TestConstructorException { // Constructor. public TestConstructorException( ) { // Throw an exception. throw new ExceptionSubClass( "TestConstructorException" ); } }